package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Override
    public Boolean saveSetemal(SetmealDTO setmealDTO) {
        Integer id = setmealDTO.getId();

        if (StringUtils.isNotBlank(String.valueOf(id))) {
            //根据id删除中间表数据
            baseMapper.deleteBySetmealId(id);
        }

        boolean flag = save(setmealDTO);
        Integer sid = setmealDTO.getId();
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        for (Integer checkgroupId : checkgroupIds) {
            baseMapper.addCheckgroupIdBySetmealId(sid,checkgroupId);
        }
        //set 集合移除提交的图片信息 ----剩下垃圾图片信息
        System.out.println("setmealDTO.getImg() = " + setmealDTO.getImg());
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        return flag;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page = null;
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        if (queryPageBean.getQueryString() == null) {
            page= page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()));

        }else {
            queryWrapper.like("helpCode",queryPageBean.getQueryString());
           page=page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());

        return pageResult;
    }

    @Override
    public void clearOssImg() {
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if (members.size() != 0) {
            for (String member : members) {
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC+member);
                if (existsKey == false) {
                    AliyunUtils.deleteFile(member);
                    System.out.println("----阿里云删除垃圾图片一次-----");
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,member);
                }
            }
        }
    }

    @Override
    public SetmealVO findSetMealDetail(Integer id) {
        SetmealVO setmealVO = baseMapper.findSetMealDetail(id);

        List<CheckGroupVO> checkGroupVOList = baseMapper.findCheckGroup(id);
        for (CheckGroupVO checkGroupVO : checkGroupVOList) {
            Integer checkGroupVOId = checkGroupVO.getId();
            List<CheckItem> checkItems =baseMapper.findCheckItemByCheckGroupVOId(checkGroupVOId);
            checkGroupVO.setCheckItemList(checkItems);
        }
        setmealVO.setCheckGroups(checkGroupVOList);
        return setmealVO;
    }

    @Override
    public SetmealVO findById(Integer id) {
        SetmealVO setmealVO = baseMapper.findById(id);
        return setmealVO;
    }


}
